package com.alipay.app.groupmeal.standard.ui.b.function.result;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.listener.ResultKeyEventListener;
import com.alipay.app.groupmeal.standard.widget.business.BaseBScreenView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.functions.Consumer;
import lombok.Data;

public class BScreenResult extends BaseBScreenView {

    private BScreenSuccessResult successResult;
    private BScreenFailResult failResult;
    private BScreenUnknownResult unknownResult;

    private ResultKeyEventListener listener;

    private Disposable mdDisposable;

    public BScreenResult(Context context) {
        super(context);
    }

    public BScreenResult(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_result;
    }

    @Override
    protected void initView(View view) {
        successResult = findViewById(R.id.success_result);
        failResult = findViewById(R.id.fail_result);
        unknownResult = findViewById(R.id.unknown_result);
    }

    @Override
    protected void initData() {

    }

    @Override
    public boolean onKeyEventEnter(KeyEvent event) {
        if (listener != null) {
            listener.enterKey();
        }
        return super.onKeyEventEnter(event);
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == GONE) {
            if (mdDisposable != null && !mdDisposable.isDisposed()) {
                mdDisposable.dispose();
                mdDisposable = null;
            }
        }
    }

    public void showPaySuccess(String amount, List<ResultBean> list, ResultKeyEventListener eventListener) {
        this.showSuccess(true, "支付成功", amount, list, eventListener);
    }

    public void showPaySuccess(boolean isAlipayIcon, String amount, List<ResultBean> list, ResultKeyEventListener eventListener) {
        this.showSuccess(isAlipayIcon, "支付成功", amount, list, eventListener);
    }

    public void showRefundSuccess(String amount, List<ResultBean> list, ResultKeyEventListener eventListener) {
        this.showSuccess(true, "退款成功", amount, list, eventListener);
    }

    /**
     * 成功结果
     *
     * @param isAlipayIcon
     * @param titleName
     * @param amount
     * @param list
     * @param eventListener
     */
    public void showSuccess(boolean isAlipayIcon, String titleName, String amount, List<ResultBean> list, ResultKeyEventListener eventListener) {
        this.listener = eventListener;
        successResult.show();
        failResult.dismiss();
        unknownResult.dismiss();

        successResult.setIcon(isAlipayIcon);
        successResult.setTitleName(titleName);
        successResult.setAmount(amount);
        successResult.setAmountVisible(View.VISIBLE);
        successResult.setActionButton("返回");

        BaseQuickAdapter<ResultBean, BaseViewHolder> adapter = new BaseQuickAdapter<ResultBean, BaseViewHolder>(R.layout.item_result_success) {
            @Override
            protected void convert(@NonNull BaseViewHolder holder, ResultBean resultBean) {
                holder.setText(R.id.txt_key, resultBean.key).setText(R.id.tv_value, resultBean.value);
            }
        };
        successResult.setRecyclerAdapter(adapter);
        adapter.setList(list);
    }


    /**
     * 核销
     *
     * @param list
     * @param eventListener
     */
    public void showWriteOff(List<ResultBean> list, ResultKeyEventListener eventListener) {
        this.listener = eventListener;
        successResult.setVisibility(VISIBLE);
        failResult.setVisibility(GONE);
        unknownResult.setVisibility(GONE);

        successResult.setIcon(true);
        successResult.setTitleName("核销成功");
        successResult.setAmountVisible(View.GONE);
        successResult.setActionButton("返回");

        BaseQuickAdapter<ResultBean, BaseViewHolder> adapter = new BaseQuickAdapter<ResultBean, BaseViewHolder>(R.layout.item_result_success) {
            @Override
            protected void convert(@NonNull BaseViewHolder holder, ResultBean resultBean) {
                holder.setText(R.id.txt_key, resultBean.key).setText(R.id.tv_value, resultBean.value);
            }
        };
        adapter.setList(list);
        successResult.setRecyclerAdapter(adapter);
    }

    public void showFail(String msg, ResultKeyEventListener eventListener) {
        this.showFail("支付失败", msg, eventListener);
    }

    /**
     * 失败结果
     *
     * @param title
     * @param msg
     * @param eventListener
     */
    public void showFail(String title, String msg, ResultKeyEventListener eventListener) {
        this.listener = eventListener;
        successResult.setVisibility(GONE);
        failResult.setVisibility(View.VISIBLE);
        unknownResult.setVisibility(GONE);

        failResult.setTitleAndMessage(title, msg);
        failResult.setActionButton("重试");
    }

    public void showUnknown(String msg, ResultKeyEventListener eventListener) {
        this.showUnknown("支付结果未知", msg, eventListener);
    }


    /**
     * 未知结果
     *
     * @param title
     * @param msg
     * @param eventListener
     */
    public void showUnknown(String title, String msg, ResultKeyEventListener eventListener) {
        this.listener = eventListener;
        successResult.setVisibility(GONE);
        failResult.setVisibility(GONE);
        unknownResult.setVisibility(View.VISIBLE);

        unknownResult.setTitleAndMessage(title, msg);
        unknownResult.setActionButton("重试");
    }


    /**
     * 显示倒计时秒数
     *
     * @param time
     */
    public void setIntervalTime(String time) {
        successResult.setTime(time);
    }

    /**
     * 倒计时
     *
     * @param count
     */
    private void intervalRange(int count) {
        mdDisposable = Flowable.intervalRange(0, count, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        successResult.setTime((count - aLong) + "秒");
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        successResult.setVisibility(GONE);
                        setVisibility(GONE);
                    }
                })
                .subscribe();
    }

    @Data
    public static class ResultBean implements Serializable {
        public String key;
        public String value;

        public ResultBean(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }
}
